/*
 * StockItemDialog.cpp
 *
 *  Created on: 05.04.2009
 *      Author: Konan Andrej
 */

#include "StockItemDialog.h"

StockItemDialog::StockItemDialog( QWidget* parent ) : QDialog( parent )
{
	ArticleLabel = new QLabel(tr("Article"));
	DescriptionLabel = new QLabel(tr("Description"));
	FormatLabel = new QLabel(tr("Format"));

	LabelsLayout = new QVBoxLayout;
	LabelsLayout->addWidget( ArticleLabel );
	LabelsLayout->addWidget( DescriptionLabel );
	LabelsLayout->addWidget( FormatLabel );


	ArticleLineEdit = new QLineEdit();
	DescriptionLineEdit = new QLineEdit();
	FormatComboBox = new QComboBox();
	FormatComboBox->setEditable( true );
	FormatComboBox->addItems( Formats );

	EditLinesLayout = new QVBoxLayout();
	EditLinesLayout->addWidget( ArticleLineEdit );
	EditLinesLayout->addWidget( DescriptionLineEdit );
	EditLinesLayout->addWidget( FormatComboBox );

	EditFieldLayout = new QHBoxLayout;
	EditFieldLayout->addLayout( LabelsLayout );
	EditFieldLayout->addLayout( EditLinesLayout );


	OkButton = new QPushButton(tr("OK"));
	CancelButton = new QPushButton(tr("Cancel"));

	ControlButtonsLayout = new QHBoxLayout;
	ControlButtonsLayout->addWidget( OkButton );
	ControlButtonsLayout->addWidget( CancelButton );


	StockItemDialogLayout = new QVBoxLayout;
	StockItemDialogLayout->addLayout( EditFieldLayout );
	StockItemDialogLayout->addLayout( ControlButtonsLayout );

	this->setLayout( StockItemDialogLayout );

	this->setWindowTitle( tr("Item data") );

	connect( OkButton, SIGNAL( clicked() ), this, SLOT( okPressed() ) );
	connect( CancelButton, SIGNAL( clicked() ), this, SLOT( close() ) );

	//ArticleLineEdit->focusWidget();
	//connect( ArticleLineEdit, SIGNAL( returnPressed() ), DescriptionLineEdit, SLOT( setFocus() ) );
	//connect( DescriptionLineEdit, SIGNAL( returnPressed() ), FormatComboBox, SLOT( setFocus() ) );
	//connect( FormatComboBox, SIGNAL( returnPressed() ), OkButton, SLOT( setFocus() ) );
}

//void StockItemDialog::MakeStockItem()
void StockItemDialog::okPressed()
{
	//Print("OkPressed() slot is executed");

	StockItem stockItem;
	stockItem.SetArticle( ArticleLineEdit->text() );
	stockItem.SetDescription( DescriptionLineEdit->text() );
	stockItem.SetFormat( FormatComboBox->currentText() );
	stockItem.SetNumber( 0 );

	//Print("Emiting StockItemMade signal");
	emit StockItemMade( stockItem );
}

void StockItemDialog::SetItem( const StockItem& item )
{
	if ( item.GetArticle() == "" ) {
		ArticleLineEdit->clear();
		ArticleLineEdit->setEnabled( true );

		DescriptionLineEdit->clear();
		FormatComboBox->clearEditText();
	} else {
		ArticleLineEdit->setText( item.GetArticle() );
		ArticleLineEdit->setEnabled( false );

		DescriptionLineEdit->setText( item.GetDescription() );
		FormatComboBox->setEditText( item.GetFormat() );
	}
}
